package com.company.cloud.product.modules.bus.producer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.company.cloud.product.modules.base.entity.ProductInfo;
import com.company.common.server.config.cloud.ProjectConfig;
import com.company.common.server.config.mq.constant.MQTopicKeys;
import com.company.common.server.config.mq.models.MQPrdModel;
import com.company.common.server.config.mq.utils.SaasMqUtils;
import com.company.common.server.constant.CloudConstant.EntityOpWayEnum;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Service
public class ProductProducer {

    @Autowired
    private ProjectConfig projectConfig;

    /**
     * 删除商品
     */
    public void delPrd(ProductInfo product) {
        this.changePrd(product, EntityOpWayEnum.ENTITY_DEL);
    }

    /**
     * 新增商品
     */
    public void addPrd(ProductInfo product) {
        this.changePrd(product, EntityOpWayEnum.ENTITY_ADD);
    }

    /**
     * 修改商品
     */
    public void editPrd(ProductInfo product) {
        this.changePrd(product, EntityOpWayEnum.ENTITY_EDIT);
    }

    /**
     * 商品上架
     */
    public void upPrd(ProductInfo product) {
        this.changePrd(product, EntityOpWayEnum.ENTITY_UP);
    }

    /**
     * 商品下架
     */
    public void downPrd(ProductInfo product) {
        this.changePrd(product, EntityOpWayEnum.ENTITY_DOWN);
    }

    /**
     * 商品变动
     */
    public void changePrd(ProductInfo product, EntityOpWayEnum opWay) {
        if (product != null) {
            MQPrdModel prd = getMQPrdModel(product, opWay);
            log.debug("+++++商品变化MQ发送消息");
            SaasMqUtils.produce(MQTopicKeys.MQ_PRD_Change_Topic, JSON.toJSONString(prd), true, projectConfig);
        }
    }

    private MQPrdModel getMQPrdModel(ProductInfo product, EntityOpWayEnum opWay) {
        MQPrdModel data = new MQPrdModel();
        BeanUtil.copyProperties(product, data);
        data.setOpWay(opWay.getCode());
        return data;
    }


}
